package com.starlink.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import org.springframework.data.domain.Pageable;
import com.starlink.common.utils.AssertUtil;
import com.starlink.model.dtomapper.MallRefundPriceDtoMapper;
import com.starlink.model.domain.MallRefundPriceDO;
import com.starlink.mapper.MallRefundPriceMapper;
import com.starlink.model.request.MallRefundPriceRequest;
import com.starlink.service.MallRefundPriceService;
import com.starlink.model.util.MallRefundPriceConvert;
import com.starlink.model.vo.MallRefundPriceVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.starlink.utils.IDUtils.getId;
import static com.starlink.utils.PageUtils.getOrderBy;

import java.util.List;

/**
 * @author zhanghong
 */
@Service
public class MallRefundPriceServiceImpl extends ServiceImpl<MallRefundPriceMapper, MallRefundPriceDO> implements MallRefundPriceService {
    @Autowired
    private MallRefundPriceMapper mallRefundPriceMapper;

    @Override
    public MallRefundPriceVO getByKey(long id) {
        MallRefundPriceVO mallRefundPriceVO = MallRefundPriceDtoMapper.INSTANCE.do2VO(getById(id));
        return mallRefundPriceVO;
    }

    @Override
    public PageInfo<MallRefundPriceVO> pageQuery(Pageable pageable,MallRefundPriceRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), getOrderBy(pageable.getSort())).setReasonable(true);
        List<MallRefundPriceDO> list = mallRefundPriceMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<MallRefundPriceVO> mallRefundPriceVOList = MallRefundPriceConvert.buildMallRefundPriceVoList(list);
        return new PageInfo(mallRefundPriceVOList);
    }

    @Override
    public List<MallRefundPriceVO> listQuery(MallRefundPriceRequest request) {
        List<MallRefundPriceDO> list = mallRefundPriceMapper.selectDOList(MallRefundPriceDtoMapper.INSTANCE.request2DO(request));
        List<MallRefundPriceVO> mallRefundPriceVOS = MallRefundPriceConvert.buildMallRefundPriceVoList(list);
        return mallRefundPriceVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(MallRefundPriceRequest request) {
        request.setId(getId());
        save(MallRefundPriceDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByKey(MallRefundPriceRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(MallRefundPriceDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDOByIds(String ids) {
        mallRefundPriceMapper.deleteDOByIds(Convert.toStrArray(ids));
        return true;
    }

    @Override
    public Integer selectCountByOrderId(String orderNum) {
        return mallRefundPriceMapper.selectCountByOrderId(orderNum);
    }
}
